package com.uetoken.cn.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.uetoken.cn.R;
import com.uetoken.cn.adapter.model.DenominationModel;
import com.uetoken.cn.adapter.model.PageNumBottonModel;
import com.uetoken.cn.adapter.model.WalletsModel;
import com.uetoken.cn.manager.UEDataManager;

import java.util.List;

public class DenominationAdapter extends RecyclerView.Adapter<DenominationAdapter.MyViewHolder> {

    private Context mContext;
    private List<DenominationModel> mDenominationModelList;
    private int mAmountInt;
//    private OnItemClickListener mClickListener;


    public DenominationAdapter(Context context, List<DenominationModel> denominationModelList) {
        this.mContext = context;

    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_denomination, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        final DenominationModel mPageNumBottonModel = mDenominationModelList.get(position);
        holder.mDenominationTv.setText(mPageNumBottonModel.getDenomination() + mContext.getResources().getString(R.string.denomination_txt));
        holder.mAmountEt.setText(mPageNumBottonModel.getQuantity() + "");

        holder.mDecreaseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int mAmount = Integer.parseInt(holder.mAmountEt.getText() + "");
                if (mAmount == 0) {
                    mAmountInt = 0;
                } else {
                    mAmountInt = mAmount - 1;
                }
                holder.mAmountEt.setText(mAmountInt + "");
                mPageNumBottonModel.setQuantity(mAmountInt);
            }
        });

        holder.mIncreaseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int mAmount = Integer.parseInt(holder.mAmountEt.getText() + "");
                mAmountInt = mAmount + 1;
                holder.mAmountEt.setText(mAmountInt + "");
                mPageNumBottonModel.setQuantity(mAmountInt);
            }
        });

        holder.mDecreaseBtn.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                LogUtils.dTag("DenominationAdapter", "DenominationAdapter 1 hasFocus = " + hasFocus);
                if (hasFocus) {
                    // 获得焦点
                    holder.mDecreaseBtn.setBackgroundResource(R.drawable.blue_semicircle_shape_1);
                } else {
                    // 失去焦点
                    holder.mDecreaseBtn.setBackgroundResource(R.drawable.border_circular_bead_shape_4);
                }

            }
        });

        holder.mIncreaseBtn.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                LogUtils.dTag("DenominationAdapter", "DenominationAdapter 2 hasFocus = " + hasFocus);
                if (hasFocus) {
                    // 获得焦点
                    holder.mDecreaseBtn.setBackgroundResource(R.drawable.blue_semicircle_shape_2);
                } else {
                    // 失去焦点
                    holder.mDecreaseBtn.setBackgroundResource(R.drawable.border_circular_bead_shape_5);
                }

            }
        });

//        holder.mIncreaseBtn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
//            @Override
//            public void onFocusChange(View v, boolean hasFocus) {
//
//            }
//        });
    }

    @Override
    public int getItemCount() {
        mDenominationModelList = UEDataManager.getInstance().getDenominationModelList();
        return mDenominationModelList == null ? 0 : mDenominationModelList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView mDenominationTv;
        private Button mDecreaseBtn;
        private EditText mAmountEt;
        private Button mIncreaseBtn;

        public MyViewHolder(View itemView) {
            super(itemView);
            mDenominationTv = (TextView) itemView.findViewById(R.id.denominationTv);
            mDecreaseBtn = (Button) itemView.findViewById(R.id.btnDecrease);
            mAmountEt = (EditText) itemView.findViewById(R.id.etAmount);
            mIncreaseBtn = (Button) itemView.findViewById(R.id.btnIncrease);
        }
    }

//    public interface OnItemClickListener {
//        public void onItemClick(View view, int postion);
//    }
//
//    public void setOnItemClickListener(PageNumBottonAdapter.OnItemClickListener listener) {
//        this.mClickListener = listener;
//    }
}
